#include "Dict.h"
#include <json/json.h>
#include <fstream>
#include <tchar.h>
#include "StringUtil.h"
bool Dict::loadFromFile(const std::filesystem::path &file_path)
{
    bool bRet = true;
    dicts_.clear();
    // std::wstring a;
    // std::ifstream file1(a);
    std::ifstream file(file_path);
    Json::Value data;
    if (file.is_open())
    {
        try
        {
            file >> data; // 使用擝作符直接读坖
            // 或者使用 Reader
            // Json::CharReaderBuilder builder;
            // std::string errors;
            // Json::parseFromStream(builder, file, &data, &errors);

            // info
            dict_info_.name_ = StringUtil::utf8ToWide(data["info"]["name"].asString());
            dict_info_.tolerance_ = StringUtil::utf8ToWide(data["info"]["tolerance"].asString());
            dict_info_.color_ = StringUtil::utf8ToWide(data["info"]["color"].asString());
            dict_info_.mode_ = (DictMode)data["info"]["mode"].asInt();

            for (const auto &dict_json : data["dicts"])
            {
                DictContent dict;
                dict.name_ = StringUtil::utf8ToWide(dict_json["name"].asString());
                dict.res_name_ = StringUtil::utf8ToWide(dict_json["res_name"].asString());
                // dict.tolerance_ = dict_json["tolerance"].asString();
                // dict.color_ = dict_json["color"].asString();
                dicts_[dict.name_] = dict;
            }
        }
        catch (const Json::Exception &e)
        {
            bRet = false;
        }
        file.close();
    }
    else
    {
        return false;
    }
    return bRet;
}

bool Dict::saveToFile(const std::filesystem::path &file_path) const
{
    Json::Value root;

    // info
    Json::Value info;
    info["name"] = StringUtil::wideToUtf8(dict_info_.name_);
    info["tolerance"] = StringUtil::wideToUtf8(dict_info_.tolerance_);
    info["color"] = StringUtil::wideToUtf8(dict_info_.color_);
    info["mode"] = dict_info_.mode_;
    root["info"] = info;

    Json::Value dicts;
    for (const auto &pair : dicts_)
    {
        const DictContent &dict = pair.second;
        Json::Value dict_json;
        dict_json["name"] = StringUtil::wideToUtf8(dict.name_);
        dict_json["res_name"] = StringUtil::wideToUtf8(dict.res_name_);
        // dict_json["tolerance"] = dict.tolerance_;
        // dict_json["color"] = dict.color_;
        dicts.append(dict_json);
        // dicts[StringUtil::wideToUtf8(dict.name_)] = dict_json;
    }
    root["dicts"] = dicts;

    std::ofstream file(file_path);
    if (file.is_open())
    {
        Json::StreamWriterBuilder builder;
        builder.settings_["indentation"] = "  "; // 设置缩进
        builder.settings_["emitUTF8"] = true;
        std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
        writer->write(root, &file);
        file.close();
    }
    else
    {
        return false;
    }
    return true;
}

void Dict::addDict(const DictContent &dict)
{
    dicts_[dict.name_] = dict;
}

bool Dict::setDictInfo(const DictInfo &dict_info)
{
    // if (dict_info_.tolerance_.empty())
    {
        dict_info_ = dict_info;
        return true;
    }
    return false;
}
